#pragma once

#include "stdafx.h"

#include "ui_sensor_editor.h"
#include "items.h"
#include "indicator.h"
#include "parameters_model.h"
#include "scene.h"

class ParametersModel;
class Common;
class Sensor;
class Delegate;
class SceneView;

class SensorEditor : public QDialog
{
	Q_OBJECT
private:
	Ui::SensorEditorDialog ui;
	QWeakPointer<Common> common;
	QWeakPointer<SceneView> scene;
	void setModel(QSharedPointer<ParametersModel> _model);
	vector<Sensor*> list_of_sensor;
	QStringList parameters;
	double pipe_length;
	ItemScene* pipe;
public:
	SensorEditor(QWeakPointer<Common> _common, QWeakPointer<SceneView> _scene);	
	void setData(vector<Sensor*> _list_of_sensor, ItemScene* _pipe/*, double _pipe_length = -1*/);
	void setSensor(Sensor *_sensor);
	vector<Sensor*> getData();
	/*double getPipeLength(){ return pipe_length; };*/
	int getIndex();
private slots:
	void on_bAdd_clicked();
	void on_bRemove_clicked();
	void displayParameters(int index);
	void displayModel(int index);
	void parameterStateChanged(QListWidgetItem *item);
	void updateSensorName(const QString &name);
	void itemClicked(QListWidgetItem *item);
};

//============================================================================================================

class Sensor : public QGraphicsObject
{
	Q_OBJECT
public:
	Sensor(QWeakPointer<Common> _common, QWeakPointer<SceneView> _scene, const QString &_name, const QStringList &_parameters = QStringList());
	Sensor::~Sensor();
	void init_model();
	QSharedPointer<ParametersModel> init_parameter_model(const QString &_parameter);
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void adjust(const QPointF &_pos);
	QString getName(){ return name; }
	bool isVisibleOnScene(){ return model->getStringParameter("DISPLAYED") == "Yes";}
	void setVisibleOnScene(const bool &_visible){
		setNewParameterValue("DISPLAYED", _visible); 
		if (_visible){
			scene.data()->addItem(this);
		}else{
			scene.data()->removeItem(this);
		}
		indicator->setIndicatorVisible(_visible);
	}
	QSharedPointer<ParametersModel> getParameterModel(QString _name);
	QSharedPointer<ParametersModel> getModel(){ return model; }
	bool getState(QString _name);
	void setIndicator(QSharedPointer<Indicator> _indicator){
		indicator = _indicator; indicator->adjust(this->pos()); 
		connect(indicator.data(), SIGNAL(createTrend(const QString&)), this, SLOT(createTrend(const QString&)));
	}
	vector<pair<QString, bool>> getListOfParameter();
	void setParameterState(const QString &_name, const bool &_state);
	QString getParameter(const QString &_name){ return model.data()->getStringParameter(_name); };
	double getNumParameter(const QString &_name){ return model.data()->getNumParameter(_name); };
	const hash_map<string, pair<QSharedPointer<ParametersModel>, bool> > & getListOfParameters(){ return list_of_parameters; }
	QPointF getIndicatorPos(){ return this->pos() - indicator->pos(); }
	QSharedPointer<Indicator> getIndicator(){ return indicator; }
	QSharedPointer<ParametersModel> addParameter(const QString &_name, const bool &_calculated);
	double getParameterError(const string &_parameter, const QString &_script, const double &_time);
	QStringList getCalculatedParameters();
	bool isCalculatedParameters(const string &_name){ return list_of_parameters[_name].second; }
	double getCoordinate(){ return getNumParameter("COORDINATE"); }
	int getDigits(const string &_name){ return list_of_parameters[_name].first->getNumParameter("DIGITS"); } 
	QString getUnit(const string &_name){ return list_of_parameters[_name].first->getStringParameter("UNIT"); } 
	int countCalcParameters();
private:
	//bool visible;
	QWeakPointer<Common> common;
	QWeakPointer<SceneView> scene;
	QString name;
	QSharedPointer<Indicator> indicator;
	QSharedPointer<ParametersModel> model;
	hash_map<string, pair<QSharedPointer<ParametersModel>, bool> > list_of_parameters;
private slots:
	void update_parameters();
	void getNewParameterValue(const QString &_name, const QVariant &_value);
	void showIndicator();
	void showIndicatorLine();
	void showSensorEditor();
	void createTrend(const QString &_parameter);
	void removeScriptParameter(const QString &script_name);
	void addScriptParameter(const QString &script_name);
protected:
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
signals:
	void updateName(const QString &_name);
	void updatePosition(Sensor* _sensor);
	void setModelData(const int &parent_row, const int &row, const QVariant &value);
	void setNewParameterValue(const QString &_name, const QVariant &_value);
};
